<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/6/2
 * Time: 下午7:02
 */

namespace App\Http\Controllers\xx;

use App\Http\Controllers\BaseController;
use App\Logic\xx\FoodLogic;
use Validator;
use Illuminate\Http\Request;

class MemberController extends BaseController
{

    public function __construct()
    {
        $this->setToken(true);
        parent::__construct();
    }

    /**
     * 收藏
     *
     * @param Request $request
     * @return array
     */
    public function collect(Request $request)
    {
        $params = $request->all();
        $rule = [
            'id' => 'required|int',
        ];
        $message = [
            'id.required' => 'ID不能为空',
        ];
        $validator = Validator::make($params, $rule, $message);
        $errors = $validator->errors()->all();
        if (!empty($errors) && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        $params['member_id'] = $this->userId;
        return FoodLogic::collect($params);
    }

    /**
     * 收藏列表
     *
     * @param Request $request
     * @return array
     */
    public function getCollectList(Request $request)
    {
        $params = $request->all();
        $rule = [
            'pagesize' => 'required|int',
            'last_id' => 'required|int',
        ];
        $message = [
            'pagesize.required' => '每页个数不能为空',
            'last_id.required' => '页码不能为空',
        ];
        $validator = Validator::make($params, $rule, $message);
        $errors = $validator->errors()->all();
        if ($errors && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        return FoodLogic::getCollectList($params, $this->userId);
    }

    /**
     * 发布
     * @param Request $request
     * @return array
     */
    public function publish(Request $request)
    {
        $params = $request->all();
        $rule = [
            'type' => 'required|int',
            'title' => 'required|string',
            'content' => 'required|string',
            'image' => 'required|string',
        ];
        $message = [
            'type.required' => '请选择类型',
            'title.required' => '标题不能为空',
            'content.required' => '内容不能为空',
            'image.required' => '图片不能为空',
        ];
        $validator = Validator::make($params, $rule, $message);
        $errors = $validator->errors()->all();
        if (!empty($errors) && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        $params['member_id'] = $this->userId;
        return FoodLogic::publish($params);
    }

    public function publishList(Request $request){
        $params = $request->all();
        $rule = [
            'pagesize' => 'required|int',
            'last_id' => 'required|int',
        ];
        $message = [
            'pagesize.required' => '每页个数不能为空',
            'last_id.required' => '页码不能为空',
        ];
        $validator = Validator::make($params, $rule, $message);
        $errors = $validator->errors()->all();
        if ($errors && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        $params['member_id'] = $this->userId;
        return FoodLogic::publishList($params);
    }
}